from flask import Flask,render_template,request
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.orm import Mapped,mapped_column
import json

app = Flask(__name__)

app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///H:/sxs/sms02/stu.db'
db = SQLAlchemy(app)
# db.session.execute("select * from stu_info")
class StuInfo(db.Model):
    __tablename__ = 'stu_info'
    sid:Mapped[int] = mapped_column(primary_key = True)
    name:Mapped[str] = mapped_column(unique=False,nullable=False)
    sex:Mapped[int] = mapped_column()
    class_name:Mapped[str] = mapped_column(unique=False, nullable=False)
    phone:Mapped[str] = mapped_column(unique=False, nullable=False)
    grade:Mapped[int] = mapped_column(unique=False, nullable=False)
    major_name:Mapped[str] = mapped_column(unique=False, nullable=False)
    college_name:Mapped[str] = mapped_column(unique=False, nullable=False)
    email:Mapped[str] = mapped_column(unique=False, nullable=False)
    password:Mapped[str] = mapped_column(unique=False, nullable=False)
@app.route('/')
def hello_world():
    return 'Hello World!'

@app.route('/login')
def login():
    return render_template("login.html")

@app.route('/gologin',methods=['post'])
def gologin():
    userName = request.form.get("userName")
    password = request.form.get("password")
    print(userName,password)
    # 从数据库中查询一条记录，来对比是否正确
    #stus = db.session.execute(db.select(StuInfo))
    # stus = db.session.execute(db.select(StuInfo)).scalars()
    # 查询所有 stus = StuInfo.query.all();
    # 按条件查询 stu = StuInfo.query.filter(StuInfo.sid == userName)
    # 按条件查询 stu = StuInfo.query.filter_by(sid = userName)
    # stus = StuInfo.query.all();
    # for stu in stus:
    #     print(stu.name)

    stu = StuInfo.query.filter(StuInfo.sid == userName,StuInfo.password == password).first()
    msg = ""
    if stu:
        msg = '登录成功'
        return render_template("index.html",loginUser=stu)
    else:
        msg = '用户名或密码错误'
        return render_template("loginerror.html")
if __name__ == '__main__':

    app.run()
